/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        if(head==nullptr||head->next==nullptr||head->next->next==nullptr)return ;
        ListNode*fast=head,*slow=head;
        while(fast&&fast->next)
        {
            slow=slow->next;
            fast=fast->next->next;
        }
        ListNode* head2=new ListNode(0);
        ListNode* cur=slow->next;
        slow->next=nullptr;
        while(cur)
        {
            ListNode* next=cur->next;
            cur->next=head2->next;
            head2->next=cur;
            cur=next;
        }
        ListNode* ret=new ListNode(0);
        ListNode*prev=ret;
        ListNode*cur1=head,*cur2=head2->next;
        while(cur1)
        {
            prev->next=cur1;
            cur1=cur1->next;
            prev=prev->next;
            if(cur2)
            {
                prev->next=cur2;
                prev=prev->next;
                cur2=cur2->next;
            }
        }
        delete head2;
        delete ret;
    }
};
